package com.wbl.tijiancms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wbl.tijiancms.mapper.CheckitemdetailedMapper;
import com.wbl.tijiancms.mapper.SetmealMapper;
import com.wbl.tijiancms.pojo.*;
import com.wbl.tijiancms.response.ApiRestResponse;
import com.wbl.tijiancms.response.TijianExceptionEnum;
import com.wbl.tijiancms.service.OrdersService;
import com.wbl.tijiancms.mapper.OrdersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author yy
* @description 针对表【orders】的数据库操作Service实现
* @createDate 2024-09-18 10:09:07
*/
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders>
    implements OrdersService{

    @Autowired
    OrdersMapper ordersMapper;


    @Autowired
    SetmealMapper setmealMapper;

    @Autowired
    CheckitemdetailedMapper checkitemdetailedMapper;


    @Override
    public ApiRestResponse ConfirmOrder(Orders order) {
        int i = ordersMapper.insertOrder(order);
        if(i>0){
            return ApiRestResponse.success(order);
        }else {
            return ApiRestResponse.error(TijianExceptionEnum.SYSTEM_ERROR);
        }
    }

    @Override
    public ApiRestResponse getOrderlistforuser(Orders order) {
        List<Orders> orders = ordersMapper.getOrderForSetmeal(order.getUserid());
        return ApiRestResponse.success(orders);
    }

    @Override
    public ApiRestResponse getOrderById(Orders order) {
        System.out.println(order);
        Orders orders = ordersMapper.selectByid(order.getOrderid());
        orders.setSetmeal(setmealMapper.selectdetailById(orders.getSmid()));
        for (Checkitem checkitem :orders.getSetmeal().getCheckitems() ){
            QueryWrapper<Checkitemdetailed> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("ciId",checkitem.getCiid());
            checkitem.setCheckitemdetaileds(checkitemdetailedMapper.selectList(queryWrapper));
        }
        return ApiRestResponse.success(orders);
    }

    @Override
    public ApiRestResponse getOrderAll(Orders order) {
        System.out.println(order);
        // 确保 user 对象已经被正确初始化
        if (order.getUser() == null) {
            order.setUser(new Users());
        }
        List<Orders> orders = ordersMapper.getAll(order);
        return ApiRestResponse.success(orders);
    }

    @Override
    public ApiRestResponse editOrder(Orders order) {
        System.out.println(order);
        int i = ordersMapper.updateOrderState(order.getOrderid(),order.getState());
        if (i>0) return ApiRestResponse.success(order);
        else return ApiRestResponse.error(TijianExceptionEnum.SYSTEM_ERROR);
    }

}




